package org.hgvc.webapp.ets.jsf.beans.mastersetup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Branch;
import org.hgvc.webapp.ets.bo.Country;
import org.hgvc.webapp.ets.bo.State;
import org.hgvc.webapp.ets.bo.Territory;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;


public class TerritoryBean extends AbstractBean {
	public static final String BEAN_NAME="territoryBean";
	private MasterSetupService service;

	private List<SelectItem> countryList;
	private List<SelectItem> stateList;
	private String countryId;
	private String stateId;
	public static final String NAV_LIST_TERRITORY="listTerritory";
	public static final String NAV_ADD_TERRITORY="addTerritory";
	public static final String NAV_EDIT_TERRITORY="editTerritory";
	private List<Territory> territoryList;
	private Territory territory;
	private Country country;
	private State state;
	private List<Country> cList;
	private List<State> sList;
	//private String branchId;
	private List<SelectItem> branchList;
	
	
	
	public List<SelectItem> getBranchList() {
		try{
			branchList= new ArrayList<SelectItem>();
			List<Branch> list = service.getBranchList();
			for(Branch b:list){
				SelectItem item = new SelectItem(b.getBranchId(),b.getDesc());
				branchList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return branchList;
	}
	public void setBranchList(List<SelectItem> branchList) {
		this.branchList = branchList;
	}
	/*public String getBranchId() {
		return branchId;
	}
	public void setBranchId(String branchId) {
		this.branchId = branchId;
	}*/
	public State getState() {
		return state;
	}
	public void setState(State state) {
		this.state = state;
	}
	public Territory getTerritory() {
		return territory;
	}
	public void setTerritory(Territory territory) {
		this.territory = territory;
	}
	public List<Territory> getTerritoryList() {
		
		try{
			
			if(stateId!=null&&stateId.length()>0){
				territoryList =service.getTerritoryList(stateId);
			}else{
				territoryList = new ArrayList<Territory>();
			}
			
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		
		return territoryList;
	}
	public void setTerritoryList(List<Territory> territoryList) {
		this.territoryList = territoryList;
	}
	
	public List<SelectItem> getCountryList() {
		try{

			cList = service.getCountryList();
			countryList= new ArrayList<SelectItem>();
			for(Country c:cList){
				SelectItem item = new SelectItem(c.getCountryId(),c.getDesc());
				countryList.add(item);
			}

		}catch(BaseException be){
			initErrorBean(be);
		}
		return countryList;
	}
	public void setCountryList(List<SelectItem> countryList) {
		this.countryList = countryList;
	}
	public List<SelectItem> getStateList() {
		try{
			stateList= new ArrayList<SelectItem>();
			if(countryId!=null&&countryId.length()>0){
				sList = service.getStateList(countryId);
				for(State s:sList){
					SelectItem item = new SelectItem(s.getStateId(),s.getDesc());
					stateList.add(item);
				}
			}
		}catch(BaseException be){
			initErrorBean(be);
		}
		return stateList;
	}
	public void setStateList(List<SelectItem> stateList) {
		this.stateList = stateList;
	}
	public String getCountryId() {
		return countryId;
	}
	public void setCountryId(String countryId) {
		this.countryId = countryId;
	}
	public String getStateId() {
		return stateId;
	}
	public void setStateId(String stateId) {
		this.stateId = stateId;
	}
	public MasterSetupService getService() {
		return service;
	}
	public void setService(MasterSetupService service) {
		this.service = service;
	}

	public String navCreationAction(){
		territory = new Territory();
		if(stateId!=null&&stateId.length()!=0){
			
			for(State s:sList){
				if(s.getStateId().equalsIgnoreCase(stateId)){
					state = s;
					break;
				}
			}
			
			return NAV_ADD_TERRITORY.concat("_"+getRoleName());
		}else{
			initErrorBean(new BaseException(SpringUtils.getMessage("error.select.state", new Object[]{})));
			return NAV_LIST_TERRITORY.concat("_"+getRoleName());
		}
		
		
	}
	
	public String createAction(){
		try{
			territory.setCreatedBy(getThisUser());
			territory.setCreatedDate(new Date());
			territory.setIsDeleted(CommonConstant.NOT_DELETED);
			Country c = new Country();
			c.setCountryId(countryId);
			State state = new State();
			state.setCountry(c);
			state.setStateId(stateId);
			territory.setState(state);
			service.saveTerritory(territory, stateId);
			return NAV_LIST_TERRITORY.concat("_"+getRoleName());
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return NAV_ADD_TERRITORY.concat("_"+getRoleName());
	}
	
	public String navEditAction(){
		return NAV_EDIT_TERRITORY.concat("_"+getRoleName());
	}
	
	public String editAction(){
		try{
			territory.setModifiedBy(getThisUser());
			territory.setModifiedDate(new Date());
			service.updateTerritory(territory, stateId);
			return NAV_LIST_TERRITORY.concat("_"+getRoleName());
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		
		return NAV_EDIT_TERRITORY.concat("_"+getRoleName());
	}
	
	public void editListener(ActionEvent actionEvent){
		territory = null;
		UIParameter parameter = (UIParameter)actionEvent.getComponent().findComponent("territoryId");
		String territoryId = (String)parameter.getValue();
		for(Territory t:territoryList){
			if(t.getTerritoryId().equalsIgnoreCase(territoryId)){
				territory = t;
				break;
			}
		}
	}
	
	public String deleteAction(){
		try{
			
			List<Territory> deleteList = new ArrayList<Territory>();
			for(Territory t:territoryList){
				if(t.isMarkAsAction()){
					deleteList.add(t);
				}
			}
			service.deleteTerritory(deleteList);
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return NAV_LIST_TERRITORY.concat("_"+getRoleName());
	}

	public String backToListAction(){
		return NAV_LIST_TERRITORY.concat("_"+getRoleName());
	}
}
